package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import com.gc.common.base.Result;
import com.gc.common.config.Global;
import com.gc.common.support.Convert;
import com.gc.common.utils.IdUtils;
import com.gc.framework.config.ServerConfig;
import com.gc.framework.util.FileUploadUtils;
import com.gc.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.VersionControl;
import com.gc.business.service.IVersionControlService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

/**
 * 版本管理 控制层
 * 
 * @author hruiyan
 * @date 2020-12-24
 */
@Slf4j
@Controller
@RequestMapping("/business/versionControl")
public class VersionControlController extends BaseController {
    private String prefix = "business/versionControl";
	
	private final IVersionControlService versionControlService;

	@Autowired
	private ServerConfig serverConfig;
	/**
	 * 文件上传路径
	 */
	public static final String UPLOAD_PATH = "/profile/apk/";

	@Autowired
	public VersionControlController(IVersionControlService versionControlService) {
		this.versionControlService = versionControlService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:versionControl:view")
	@GetMapping
	public String versionControl(){
	    return prefix + "/versionControl";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{versionId}")
	public String edit(@PathVariable("versionId") Long versionId, ModelMap mmap){
		VersionControl versionControl = versionControlService.selectVersionControlById(versionId);
		mmap.put("versionControl", versionControl);
		return prefix + "/edit";
	}
	
	/**
	 * 查询版本管理列表
	 */
	@RequiresPermissions("business:versionControl:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(VersionControl versionControl){
		startPage();
        List<VersionControl> list = versionControlService.selectVersionControlList(versionControl);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存版本管理
	 */
	@RequiresPermissions("business:versionControl:add")
	@Log(title = "版本管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(VersionControl versionControl){
		versionControl.setVersionId(IdUtils.getPrimaryKey());
		versionControl.setCreateBy(ShiroUtils.getLoginName());
		versionControl.setCreateTime(DateUtil.date());
		versionControlService.save(versionControl);
		return Result.success();
	}

	/**
	 * 修改保存版本管理
	 */
	@RequiresPermissions("business:versionControl:edit")
	@Log(title = "版本管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(VersionControl versionControl){
		versionControlService.updateById(versionControl);
		return Result.success();
	}
	
	/**
	 * 删除版本管理
	 */
	@RequiresPermissions("business:versionControl:remove")
	@Log(title = "版本管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		versionControlService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出版本管理列表
	 */
	@RequiresPermissions("business:versionControl:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(VersionControl versionControl){
		List<VersionControl> list = versionControlService.list(versionControl);
		ExcelUtil<VersionControl> util = new ExcelUtil<>(VersionControl.class);
		util.exportExcel(list, "versionControl");
		return Result.success();
	}


	/**
	 * 上传文件
	 */
	@Log(title = "上传app安装包", businessType = BusinessType.INSERT)
	@PostMapping("/savefy")
	@ResponseBody
	public Result addSave(@RequestParam MultipartFile apkUpload)
	{
		String url = "";
		try {
			if(!apkUpload.isEmpty()) {
				String originalFilename = apkUpload.getOriginalFilename();
				String extension = originalFilename.substring(originalFilename.length() - 4);
				String fileName = FileUploadUtils.uploadApk(Global.getApkUploadPath(), apkUpload,extension);
				url = serverConfig.getUrl() + UPLOAD_PATH + fileName;
			}
		} catch (Exception e) {
			return Result.failed();
		}
		return Result.success(url);
	}
}
